package com.rapig.mail.biz;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import javazoom.jl.player.Player;

import com.rapig.mail.MailTray;
import com.rapig.mail.bean.MailAccount;

public class TrayBIZ {
	private static Image imgMail = createImage("/res/normal.gif", "mail");
	private static Image imgNewMail = createImage("/res/new.gif", "mail");
	public static final PopupMenu popup = new PopupMenu();
	public static final SystemTray tray = SystemTray.getSystemTray();
	public static final TrayIcon trayIcon = new TrayIcon(imgMail);

	public static MenuItem aboutItem = new MenuItem("About");
	public static Menu mailMenu = new Menu("Mail");
	public static MenuItem refreshItem = new MenuItem("Refresh");
	public static MenuItem settingItem = new MenuItem("Setting");
	public static MenuItem exitItem = new MenuItem("Exit");

	// Obtain the image URL
	protected static Image createImage(String path, String description) {
		URL imageURL = MailTray.class.getResource(path);

		if (imageURL == null) {
			System.err.println("Resource not found: " + path);
			return null;
		} else {
			Image img = (new ImageIcon(imageURL, description)).getImage()
					.getScaledInstance(16, 16, Image.SCALE_DEFAULT);
			return img;
		}
	}

	public static void resetTray(int total, String info) {
		String label = "Mail";
		if (total > 0) {
			label = "Mail(+" + total + ")";
			trayIcon.setImage(imgNewMail);
		} else {
			trayIcon.setImage(imgMail);
		}
		mailMenu.setLabel(label);
		trayIcon.setToolTip(label);

		if (total > 0) {
			alertMusic();
			trayIcon.displayMessage("Mail Received", info, MessageType.INFO);
		}
	}

	public static void initTray() {

		popup.removeAll();
		popup.add(aboutItem);
		popup.add(settingItem);
		popup.addSeparator();
		popup.add(mailMenu);
		popup.add(refreshItem);
		popup.addSeparator();
		popup.add(exitItem);

		for (MailAccount item : MailBIZ.accounts) {
			if (item == null) {
				continue;
			}
			MenuItem itemMenu = new MenuItem(item.getName());
			mailMenu.add(itemMenu);
		}

		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,
						"Powered by Haris(hatrim@gmail.com)");
			}
		});

		refreshItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MailTask.refresh();
			}
		});

		settingItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "hatrim@gmail.com");
			}
		});

		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TrayBIZ.tray.remove(TrayBIZ.trayIcon);
				System.exit(0);
			}
		});

		trayIcon.setPopupMenu(popup);

		try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			System.out.println("TrayIcon could not be added.");
			return;
		}

		trayIcon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "hatrim@gmail.com");
			}
		});
	}

	public static String path(String file) {
		URL fileUrl = Thread.currentThread().getContextClassLoader()
				.getResource(file);
		return fileUrl.getPath();
	}

	public static void alertMusic() {
		try {
			new Player(new FileInputStream(new File(path("res/alert.mp3"))))
					.play();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
